(function($, doc) {
	
	
		
		
	$.plusReady(function() {

		var user = app.getState();
		var uid = user.user_id;


		mui.init();


		doc.getElementById('buy_type').addEventListener('tap', function() {
			doc.getElementById('buy_list').style.display = '';
			doc.getElementById('sale_list').style.display = 'none';
			doc.getElementById('sale_type').classList.remove('buy-active');
			doc.getElementById('buy_type').classList.add('buy-active');
		});

		doc.getElementById('sale_type').addEventListener('tap', function() {
			doc.getElementById('buy_list').style.display = 'none';
			doc.getElementById('sale_list').style.display = '';
			doc.getElementById('buy_type').classList.remove('buy-active');
			doc.getElementById('sale_type').classList.add('buy-active');
			
			// 加载过切换时不重新加载
			if (!refreshSale.excuted) {
				refreshSale();
				refreshSale.excuted = 1;
			}
		});

		$('.mui-table-view').on('tap', 'li', function() {
			$.openWindow({
				url: 'gat_details.html',
				id: 'gat_details',
				extras: {
					oid: this.id
				}
			});
		});

	
		window.addEventListener('refreshbuy', function(e) {
			refreshBuy();
		});
	
		window.addEventListener('refreshsale', function(e) {
			refreshSale();
		});
	
		
		function refreshBuy() {
			gatc.lazyload('#ywc_list', {
				loadData: getBuyRecord,
				loadPage: getBuyRecordPage
			});
		}
		
		function refreshSale() {
			gatc.lazyload('#mcywc_list', {
				loadData: getSaleRecord,
				loadPage: getSaleRecordPage
			});
		}
		
	
		function getBuyRecord(page, pageSize, callback) {
	
			var user = app.getState();
			var uid = user.user_id;
			
			mui.ajax({
				url: serverurl + "Index/order/buyRecord.html",
				data: {
					'uid': uid
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					
					//console.log(JSON.stringify(res));
					if(res && res.status > 0) {
						var data = res.data;
						var list;
						
						gatc.lazyloadEnd('#wpp_list', (list = data.data5) && list.length > 0 && template('wpp-template', {
							wpplists: list
						}));
						
						gatc.lazyloadEnd('#ddk_list', (list = data.data6) && list.length > 0 && template('ddk-template', {
							ddklists: list
						}));
	
						gatc.lazyloadEnd('#qrdk_list', (list = data.data8) && list.length > 0 && template('qrdk-template', {
							qrdklists: list
						}));
						
						if((list = data.data10) && list.length > 0) {
							callback(template('ywc-template', {
								ywclists: list
							}), list.length);
						} else {
							callback('', 0);
						}
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
			
			return getBuyRecordPage;
		}
		
	
		function getBuyRecordPage(page, pageSize, callback) {
	
			var user = app.getState();
			var uid = user.user_id;
	
			mui.ajax({
				url: serverurl + "Index/order/buyRecordPages.html",
				data: {
					'uid': uid,
					'page': page,
					'size': pageSize
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					
					console.log(JSON.stringify(res));
					if(res && res.status > 0) {
						var list = res.data.data10;
						
						if(list && list.length > 0) {
							callback(template('ywc-template', {
								ywclists: list
							}), list.length);
						} else {
							callback('', 0);
						}
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
		}	
	
	
		function getSaleRecord(page, pageSize, callback) {
	
			var user = app.getState();
			var uid = user.user_id;
			console.log(uid);
			console.log(serverurl + "Index/order/saleRecord1.html");
			mui.ajax({
				url: serverurl + "Index/order/saleRecord1.html",
				data: {
					'uid': uid
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					
					console.log(JSON.stringify(res));
					if(res && res.status > 0) {
						var data = res.data;
						var list;
						
						gatc.lazyloadEnd('#dsh_list', (list = data.data1) && list.length > 0 && template('dsh-template', {
							dshlists: list
						}));
						
						gatc.lazyloadEnd('#dpp_list', (list = data.data2) && list.length > 0 && template('dpp-template', {
							dpplists: list
						}));
						
						gatc.lazyloadEnd('#dsk_list', (list = data.data3) && list.length > 0 && template('dsk-template', {
							dsklists: list
						}));
						
						if((list = data.data4) && list.length > 0) {
							callback(template('mcywc-template', {
								mcywclists: list
							}), list.length);
						} else {
							callback('', 0);
						}
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
			
			return getSaleRecordPage;
		}
	
	
		function getSaleRecordPage(page, pageSize, callback) {
	
			var user = app.getState();
			var uid = user.user_id;
	
			mui.ajax({
				url: serverurl + "Index/order/saleRecordPages.html",
				data: {
					'uid': uid,
					'page': page,
					'size': pageSize
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					
					//console.log(JSON.stringify(res));
					if(res && res.status > 0) {
						var list = res.data.data4;
						
						if(list && list.length > 0) {
							callback(template('mcywc-template', {
								mcywclists: list
							}), list.length);
						} else {
							callback('', 0);
						}
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
		}
		
		
		refreshBuy();
		
	});
		

}(mui, document));